package com.ruizhong.centercontroll.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruizhong.centercontroll.dao.T3DSceneMapper;
import com.ruizhong.centercontroll.entity.T3DMesh;
import com.ruizhong.centercontroll.entity.T3DScene;
import com.ruizhong.centercontroll.entity.TButton;
import com.ruizhong.centercontroll.service.T3DMeshService;
import com.ruizhong.centercontroll.service.T3DSceneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class T3DSceneServiceImpl extends ServiceImpl<T3DSceneMapper, T3DScene> implements T3DSceneService {

    @Autowired
    private T3DMeshService t3DMeshService;

    @Override

    public void saveNew3DList(List<Map<String, Object>> threeDSceneList, List<Map<String, Object>> threeDMeshList, List<Map<String, Object>> threeDSourceList, Integer pageId, boolean isNew) {
        delete3DSceneByPageId(pageId);
        List<T3DScene> t3DScenes = threeDSceneList.stream().map(scene -> {
            T3DScene t3DScene = new T3DScene();
            Object sceneIndex = scene.get("sceneIndex");
            Object conditionShow = scene.get("conditionShow");
            t3DScene.setPageId(pageId);
            t3DScene.setSceneIndex((Integer) sceneIndex);
            t3DScene.setW(Double.parseDouble(scene.get("w").toString()));
            t3DScene.setH(Double.parseDouble(scene.get("h").toString()));
            t3DScene.setX(scene.get("x") == null?0:Double.parseDouble(scene.get("x").toString()));
            t3DScene.setY(scene.get("y") == null?0:Double.parseDouble(scene.get("y").toString()));
            t3DScene.setZ(Integer.parseInt(scene.get("z").toString()));
            t3DScene.setConditionShowData(conditionShow == null ? null : (Boolean) conditionShow ? 1 : 0);
            return t3DScene;
        }).collect(Collectors.toList());
        saveBatch(t3DScenes);
        t3DMeshService.delete3DMeshByPageId(pageId);
        List<T3DMesh> t3DMeshes = threeDMeshList.stream().map(mesh -> {
            T3DMesh t3DMesh = new T3DMesh();
            t3DMesh.setPageId(pageId);
            t3DMesh.setSourcePath(mesh.get("sourcePath").toString());
            t3DMesh.setPositionX(Double.parseDouble(mesh.get("positionX").toString()));
            t3DMesh.setPositionY(Double.parseDouble(mesh.get("positionY").toString()));
            t3DMesh.setPositionZ(Double.parseDouble(mesh.get("positionZ").toString()));
            t3DMesh.setScalingX(Double.parseDouble(mesh.get("scalingX").toString()));
            t3DMesh.setScalingY(Double.parseDouble(mesh.get("scalingY").toString()));
            t3DMesh.setScalingZ(Double.parseDouble(mesh.get("scalingZ").toString()));
            t3DMesh.setRotationX(Double.parseDouble(mesh.get("rotationX").toString()));
            t3DMesh.setRotationY(Double.parseDouble(mesh.get("rotationY").toString()));
            t3DMesh.setRotationZ(Double.parseDouble(mesh.get("rotationZ").toString()));
              t3DMesh.setName(mesh.get("name").toString());
            t3DMesh.setSceneIndex(Integer.parseInt(mesh.get("sceneIndex").toString()));
            return t3DMesh;
        }).collect(Collectors.toList());
        t3DMeshService.saveBatch(t3DMeshes);
    }

    private void delete3DSceneByPageId(Integer pageId) {
        QueryWrapper<T3DScene> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.lambda().eq(T3DScene::getPageId, pageId);
        remove(deleteWrapper);
    }

    @Override
    public List<T3DScene> get3DsByPageId(Integer pageId, Double pageWidth, Double pageHeight, Double pageHeight1, Double aimPageWidth, Double aimPageHeight, Boolean display) {
        List<T3DScene> t3DScenes = lambdaQuery()
                .eq(T3DScene::getPageId, pageId)
                .list();
        List<T3DMesh> t3DMeshes = t3DMeshService.get3DMeshsByPageId(pageId, display);
//        Map<Integer, T3DScene> indexMapScene = t3DScenes.stream().collect(Collectors.toMap(T3DScene::getSceneIndex, scene -> scene));
        Map<Integer, T3DScene> indexMapScene = new HashMap<>();
        for (T3DScene t3DScene : t3DScenes) {
            if (aimPageHeight != -1 && aimPageWidth != -1) {
                t3DScene.setW(Math.floor((t3DScene.getW() * aimPageWidth) / pageWidth));
                t3DScene.setH(Math.floor((t3DScene.getH() * aimPageHeight) / pageHeight));
                t3DScene.setX(Math.floor((t3DScene.getX() * aimPageWidth) / pageWidth));
                t3DScene.setY(Math.floor((t3DScene.getY() * aimPageHeight) / pageHeight));
            }
            t3DScene.setConditionShow(t3DScene.getConditionShowData() == null ? false : t3DScene.getConditionShowData() == 1);
            indexMapScene.put(t3DScene.getSceneIndex(), t3DScene);
        }
        for (T3DMesh t3DMesh : t3DMeshes) {
            Integer sceneIndex = t3DMesh.getSceneIndex();
            T3DScene t3DScene = indexMapScene.get(sceneIndex);
            List<T3DMesh> meshList = t3DScene.getMeshList();
            if (CollectionUtils.isEmpty(meshList)) {
                meshList = new ArrayList<>();
                t3DScene.setMeshList(meshList);
            }
            meshList.add(t3DMesh);
            Set<String> sourceList = t3DScene.getSourceList();
            if (CollectionUtils.isEmpty(sourceList)){
                sourceList = new HashSet<>();
                t3DScene.setSourceList(sourceList);
            }
            sourceList.add(t3DMesh.getSourcePath());
        }

        return t3DScenes;
    }
}
